/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package eu.ict_omelette.mashup.adapters.pipes.database;

import eu.ict_omelette.mashup.canonicalmodel.Component;
import eu.ict_omelette.mashup.canonicalmodel.InputParameter;
import java.sql.*;
import java.util.ArrayList;

/**
 *
 * @author guybrush
 */
public class DatabaseParser {

    static final String getTemplateQry = "SELECT simple,list,cmplxList,listList FROM Template where componentName = '",
                        getDataMappedQry = "SELECT dataMapped FROM Template where componentName = '";

    static public void getTemplatedLists(String type, ArrayList<String> simple,
                                                     ArrayList<ArrayList<String>> cmplx_underscored,
                                                     ArrayList<ArrayList<String>> cmplx_pointed,
                                                     ArrayList<ArrayList<ArrayList<String>>> cmplx_both) throws SQLException
    {

        Connection conn = null;
        Statement stat = null;

        try{
            Class.forName("org.sqlite.JDBC");
            conn = DriverManager.getConnection("jdbc:sqlite:bayatest.sqlite");
            stat = conn.createStatement();

            ResultSet res = stat.executeQuery(getTemplateQry + type + "'");

            if(res.next())
            {
                String[] t_simple = res.getString(1).split(";"),
                       t_list = res.getString(2).split(";"),
                       t_cmplx_list = res.getString(3).split("\\];"),
                       t_list_list = res.getString(4).split("\\];"),
                       arr = null;
                if (!t_simple[0].isEmpty())
                    for(String s : t_simple)
                    {
                        arr = s.split(":");
                        if (!isPresent(arr[0], simple, cmplx_underscored, cmplx_pointed, cmplx_both))
                        { simple.add(arr[0]); simple.add(getEmptyInputValue(arr[1])); }
                    }
                if (!t_list[0].isEmpty())
                    for(String s: t_list)
                    {
                        arr = s.split(":");
                        if (!isPresent(arr[0], simple, cmplx_underscored, cmplx_pointed, cmplx_both))
                        {
                            ArrayList<String> tmp = new ArrayList<String>(); tmp.add(arr[0]);
                            for(int x=1;x<arr.length;x++) tmp.add(getEmptyInputValue(arr[x]));
                            cmplx_underscored.add(tmp);
                        }
                    }
                if (!t_cmplx_list[0].isEmpty())
                    for(String s : t_cmplx_list)
                    {
                        arr = s.split("\\[");
                        if (!isPresent(arr[0], simple, cmplx_underscored, cmplx_pointed, cmplx_both))
                        {
                            ArrayList<String> tmp = new ArrayList<String>(); tmp.add(arr[0]);
                            arr = arr[1].split("]")[0].split(";"); for (int x=0;x<arr.length;x++)
                            { String[] subArr = arr[x].split(":"); tmp.add(subArr[0]); tmp.add(getEmptyInputValue(subArr[1])); }
                            cmplx_pointed.add(tmp);
                        }
                    }
                if (!t_list_list[0].isEmpty())
                    for(String s : t_list_list)
                    {
                        arr = s.split("\\[");
                        if (!isPresent(arr[0], simple, cmplx_underscored, cmplx_pointed, cmplx_both))
                        {
                            ArrayList<ArrayList<String>> tmp = new ArrayList<ArrayList<String>>();
                            ArrayList<String> tmptmp = new ArrayList<String>(); tmptmp.add(arr[0]);
                            tmp.add(tmptmp);
                            arr = arr[1].split("};");
                            for (int x=0;x<arr.length;x++)
                            {
                                tmptmp = new ArrayList<String>();
                                String fields = arr[x];
                                if (fields.contains("{")) fields = fields.split("\\{")[1];
                                if (fields.contains("}")) fields = fields.split("\\}")[0];
                                String[] subArr = fields.split(";");
                                for (int y=0;y<subArr.length;y++)
                                {
                                    String[] subVal = subArr[y].split(":");
                                    tmptmp.add(subVal[0]); tmptmp.add(getEmptyInputValue(subVal[1]));
                                }
                                tmp.add(tmptmp);
                            }

                            cmplx_both.add(tmp);
                        }
                    }
            }
        }catch(Exception e)
        { e.printStackTrace(); }
        finally
        {
            if (stat != null) stat.close();
            if (conn != null) conn.close();
        }


    }

    public static Boolean isDataMapped(Component comp, InputParameter ip) throws SQLException
    {
        Connection conn = null;
        Statement stat = null;

        String name = ip.getName(); if (name.contains(".")) name = name.split("\\.")[1];

        try{
            Class.forName("org.sqlite.JDBC");
            conn = DriverManager.getConnection("jdbc:sqlite:bayatest.sqlite");
            stat = conn.createStatement();

            ResultSet res = stat.executeQuery(getDataMappedQry + comp.getName() + "'");
            if (res.next())
            {
                String dataMapped[] = res.getString(1).split(";");
                for(int x=0;x<dataMapped.length;x++)
                {
                    if (!dataMapped[x].equals(""))
                    {
                        String field = dataMapped[x];
                        if (name.equals(field) && ip.getType().equals("text")) 
                        { //System.out.println(name + " in " + comp.getName() + "\nis dataMapped field");
                          return true;
                        }
                    }
                }
            }
        }catch(Exception e)
        { e.printStackTrace(); }
        finally
        {
            if (stat != null) stat.close();
            if (conn != null) conn.close();
        }

        //System.out.println(name +" in " + comp.getName() + "\nis NOT dataMapped field");
        return false;
    }

    private static Boolean isPresent(String value, ArrayList<String> simple,
                                                   ArrayList<ArrayList<String>> cmplx_underscored,
                                                   ArrayList<ArrayList<String>> cmplx_pointed,
                                                   ArrayList<ArrayList<ArrayList<String>>> cmplx_both)
    {

        /* ArrayList<String> simple = new ArrayList<String>(); //[pair] = name of input; [odd] = value of input;
        ArrayList<ArrayList<String>> cmplx_underscored = new ArrayList<ArrayList<String>>(); //[0] = name of list; [>0] = value of input;
        ArrayList<ArrayList<String>> cmplx_pointed = new ArrayList<ArrayList<String>>(); //[0] = name of list; [odd index] = name of input; [pair index] = value of input;
        ArrayList<ArrayList<ArrayList<String>>> cmplx_both = new ArrayList<ArrayList<ArrayList<String>>>(); //[0][0] = name of list; [>0][pair index] = name of input; [>0][odd index] = value of input;
	 *
         */
        Integer sz = simple.size();
        if (sz > 0)
            for (int x=0;x<sz-1;x=x+2)
                if (simple.get(x).equals(value)) return true;

        sz = cmplx_underscored.size();
        if (sz > 0)
            for (int x=0;x<sz;x++)
                if(cmplx_underscored.get(x).get(0).equals(value)) return true;

        sz = cmplx_pointed.size();
        if (sz > 0)
            for (int x=0;x<sz;x++)
                if(cmplx_pointed.get(x).get(0).equals(value)) return true;
        sz = cmplx_both.size();
        if (sz > 0)
            for (int x=0;x<sz;x++)
                if(cmplx_both.get(x).get(0).get(0).equals(value)) return true;

        return false;
    }
    static private String getEmptyInputValue(String type)
    { return "{\"value\":\"\",\"type\":\"" + type + "\"}"; }
}
